//
//  LSEmotionStatckView.swift
//  新浪微博---01次
//
//  Created by 李宗帅 on 16/5/12.
//  Copyright © 2016年 LS. All rights reserved.
//

import UIKit
import SnapKit
let ButtonCont = 4

//是什么按钮,
enum LSEmoticonType: Int{
    
    case    Recent = 10000
    case    Default = 10001
    case    Emoji = 10002
    case    Lxh = 10003
    
}

protocol LSEmotionStatckViewDelegate: NSObjectProtocol{
    
    func didEmotionSelectedSecion(type:LSEmoticonType)
    
}

class LSEmotionStatckView: UIView {
    

    weak var delegate:LSEmotionStatckViewDelegate?
    
    var clickButton:UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
    
        addChildButtons()
        
        addSubview(recentBtn)
        addSubview(defauleBtn)
        addSubview(emojiBtn)
        addSubview(lxhBtn)
        
        recentBtn.snp_makeConstraints { (make) -> Void in
            make.top.left.bottom.equalTo(self)
        }
        
        defauleBtn.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(recentBtn)
            make.top.bottom.equalTo(self)
            make.left.equalTo(recentBtn.snp_right)
        }
        
        emojiBtn.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(defauleBtn)
            
            make.top.bottom.equalTo(self)
            
            make.left.equalTo(defauleBtn.snp_right)
            
        }
        
        lxhBtn.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(emojiBtn)
            
            make.top.bottom.equalTo(self)
            
            make.left.equalTo(emojiBtn.snp_right)
            
            make.right.equalTo(self)
        }
        
    
    }
    
    private func addChildButtons(){
        
        recentBtn = cutomButton("最近", type: .Recent)
        
        defauleBtn = cutomButton("默认", type: .Default)
        
        emojiBtn = cutomButton("emoji", type: .Emoji)
        
        lxhBtn = cutomButton("浪小花", type: .Lxh)
        
    }
    
    private func cutomButton(title: String,type: LSEmoticonType) -> UIButton{
        
        let button = UIButton(type: .Custom)

        //设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: .Selected)
        
        //添加点击事件
        button.addTarget(self, action: "showSelectedEmoticon:", forControlEvents: UIControlEvents.TouchUpInside)
        
        //代理需要区分按钮点击了哪一个.所以要加Tag
        button.tag = type.rawValue
        
        
        //设置按钮文字
        button.setTitle(title, forState: .Normal)
        
        //设置文字颜色
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Selected)
        
        //设置字体
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        
        //创建按钮的时候,默认状态是被选中的
        if type == .Default {
            
            button.selected = true
            
            clickButton = button
        }
        
        return button
    }
    
    @objc private func showSelectedEmoticon(button:UIButton){
        
        
        if clickButton == button {
            return
        }
        
        //让当前按钮取反选中
        button.selected = true
        
        //让上次点击的按钮取消被选中状态
        clickButton?.selected = false
        
        //把当前按钮保存
        clickButton = button
        
        //代理方法
        delegate?.didEmotionSelectedSecion(LSEmoticonType(rawValue: button.tag)!)
    }
    
    //懒加载
    private lazy var recentBtn = UIButton()
    
    private lazy var defauleBtn = UIButton()
    
    private lazy var emojiBtn = UIButton()
    
    private lazy var lxhBtn = UIButton()
    
    func showSelectedButton(section: Int){
        
        // section 2+10000
        let button = viewWithTag(10000+section) as! UIButton
        
        if button == clickButton {
            return
        }
        
        button.selected = true
        // 原来的按钮选中效果消失
        clickButton?.selected = false
        
        clickButton = button
        
        
    }


}
